package cc.ufinity.networkdiagnose.widget;
 
import cc.ufinity.networkdiagnose.manager.Task;
import cc.ufinity.networkdiagnose.manager.TelManager;
import cc.ufinity.networkdiagnose.manager.WlanManager; 
import cc.ufinity.networkdiagnose.view.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.net.ConnectivityManager;
import android.view.View;
import android.widget.Button;

public class SelectNetType extends GeneralTestWidget implements View.OnClickListener {
    private Button btn1;
    private Button btn2;
    private Button btn3;
    private Activity ctx;
    private Task task;         
	private TelManager tm;
    private TaskDetailWidget tdw;
    private WlanManager wlanManager;
    private Button btnBack; 
    private Test_Task_Widget ttw2;
    public SelectNetType(Activity context) {
        super(context);
        inflate(context, R.layout.nettype, this);
        btn1 = (Button) this.findViewById(R.id.button1);
        btn2 = (Button) this.findViewById(R.id.button2);
        btn3 = (Button) this.findViewById(R.id.button3);
        btnBack = (Button) this.findViewById(R.id.btn_nettype_back);
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btnBack.setOnClickListener(this);
        this.ctx = context;
        task = (Task) ctx.getIntent().getSerializableExtra(TAG_REFER_TASK);
        if(task.scanResult != null && task.scanResult.SSID.equals(WlanManager.SSID_CMCC)){
        //	showCMCCAuthDialog(context);
         }
		tm = new TelManager(ctx);
    }

    @Override
    public void onClick(View v) {
    	String from = ctx.getIntent().getStringExtra("from");
    	from = from == null?"":from;
    	if(from.equals("TestWidget")) {
    		 switch (v.getId()) {
 	        case R.id.button1:
 	            wlanManager = new WlanManager(ctx);
                wlanManager.setMobileDataEnabled(ctx,false);
 	            task.selNetType = ConnectivityManager.TYPE_WIFI;
 	            ctx.getIntent().putExtra(TAG_REFER_TASK, task); 
 	            ttw2 = new Test_Task_Widget(ctx);
 	            ctx.setContentView(ttw2); 
 	            break;
 	        case R.id.button2:
 	            wlanManager = new WlanManager(ctx);
 	            task.selNetType = ConnectivityManager.TYPE_MOBILE_MMS;
 	            if(tm.getNetworkType()!=null && tm.getNetworkType().equals("EDGE")){
					showAlertDialog(ctx,"当前网络类型为GSM模式，进行TD测试请先切换至TD网络类型.");
				}else {
	 	            wlanManager.closeWifi();
	                wlanManager.setMobileDataEnabled(ctx,true);
	 	            ctx.getIntent().putExtra(TAG_REFER_TASK, task);
	 	            ttw2 = new Test_Task_Widget(ctx);
	 	            ctx.setContentView(ttw2);
				}  
 	            break;
 	        case R.id.button3:
 	        	 wlanManager = new WlanManager(ctx);
 	             task.selNetType = ConnectivityManager.TYPE_MOBILE;
 	             if (tm.getNetworkType()!=null && tm.getNetworkType().equals("HSDPA")) {
					showAlertDialog(ctx,"当前网络类型为TD模式，进行GSM测试请先切换至GSM网络类型.");
				 }else {
	 	             wlanManager.closeWifi();
	                 wlanManager.setMobileDataEnabled(ctx,true);
	 	             ctx.getIntent().putExtra(TAG_REFER_TASK, task);
	  	             ttw2 = new Test_Task_Widget(ctx);
	  	             ctx.setContentView(ttw2);
				}
 	            break;
 	        case R.id.btn_nettype_back: 
 	    		this.back(ctx);
 	            break;
 	        }
    	} else { 
    		 switch (v.getId()) {
    	        case R.id.button1:
    	            wlanManager = new WlanManager(ctx);
    	            task.selNetType = ConnectivityManager.TYPE_WIFI;
    	            ctx.getIntent().putExtra(TAG_REFER_TASK, task); 
    	            wlanManager.setMobileDataEnabled(ctx,false);
    	            tdw = new TaskDetailWidget(ctx);
    	            ctx.setContentView(tdw);
    	        	/* wlanManager = new WlanManager(ctx);
    	            task.selNetType = ConnectivityManager.TYPE_WIFI;
    	            ctx.getIntent().putExtra(TAG_REFER_TASK, task);
    	            ttw = new Task_Test_Widget(ctx);
    	            ctx.setContentView(ttw); */
    	            break;
    	        case R.id.button2:
    	            wlanManager = new WlanManager(ctx);
    	            wlanManager.closeWifi();
    	            wlanManager.setMobileDataEnabled(ctx,true);
    	            task.selNetType = ConnectivityManager.TYPE_MOBILE;
    	            ctx.getIntent().putExtra(TAG_REFER_TASK, task);
    	            tdw = new TaskDetailWidget(ctx);
    	            ctx.setContentView(tdw);
    	            break;
    	        case R.id.button3:
    	        	 wlanManager = new WlanManager(ctx);
    	             wlanManager.closeWifi();
    	             wlanManager.setMobileDataEnabled(ctx,true);
    	             task.selNetType = ConnectivityManager.TYPE_MOBILE;
    	             ctx.getIntent().putExtra(TAG_REFER_TASK, task);
    	             tdw = new TaskDetailWidget(ctx);
    	             ctx.setContentView(tdw);
    	            break;   
    	        case R.id.btn_nettype_back: 
    	        	if(ctx.getIntent()!=null && ctx.getIntent().getSerializableExtra("TAG_CHECK_TASK")!=null && ((String)(ctx.getIntent().getSerializableExtra("TAG_CHECK_TASK"))).equals("checkTask")){
    					ctx.getIntent().putExtra("TAG_CHECK_TASK", "");
    	        		CheckWidget cw = new CheckWidget(ctx);
    					cw.start();
    					ctx.setContentView(cw);
    	        	}else
    	        		this.backToTask(ctx);
    	            break;
    	        }  
    	}
    }
     
    public void showAlertDialog(final Activity ctx,String message) { 
        AlertDialog.Builder builder = new AlertDialog.Builder(ctx); 
    	builder.setTitle("测试提示");   
    	builder.setMessage(message); 
    	builder.setCancelable(true);
 		builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
 			public void onClick(DialogInterface dialog, int id) { 
 			    wlanManager.closeWifi();
				wlanManager.setMobileDataEnabled(ctx,true); 
				ctx.getIntent().putExtra(TAG_REFER_TASK, task);
 	            ttw2 = new Test_Task_Widget(ctx);
 	            ctx.setContentView(ttw2);
 			}
 		}); 
 		builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
 			public void onClick(DialogInterface dialog, int id) { 
        		dialog.cancel();
 			}
 		});
 		wifiListDialog = builder.create();
        wifiListDialog.show();
    }
}
